package io.renren.modules.appReport.service.impl;

import io.renren.modules.appReport.controller.AppInfoController;
import io.renren.modules.appReport.dao.AppInfoDao;
import io.renren.modules.appReport.entity.AppInfoEntity;
import io.renren.modules.appReport.model.AppFinanceAuditExcelBean;
import io.renren.modules.appReport.service.AppInfoService;
import io.renren.modules.cgs.dao.CgsPassporapplyDao;
import io.renren.modules.message.dao.MessageDao;
import io.renren.modules.message.entity.MessageEntity;
import io.renren.modules.push.PushService;
import io.renren.modules.query.entity.Vehicle;
import io.renren.modules.query.service.DataInfoQueryService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("appInfoService")
public class AppInfoServiceImpl implements AppInfoService {
    @Autowired
    private AppInfoDao appInfoDao;

    @Autowired
    private CgsPassporapplyDao cgsPassporapplyDao;

    @Autowired
    private MessageDao messageDao;

    @Autowired
    private DataInfoQueryService dataInfoQueryService;

    @Autowired
    private PushService pushService;

    @Override
    public AppInfoEntity queryObject(String id) {
        AppInfoEntity appInfo = appInfoDao.queryObject(id);
        return backAppInfo(appInfo);
    }

    @Override
    public AppInfoEntity queryObject(AppInfoEntity appInfo) {
        return backAppInfo(appInfo);
    }

    @Override
    public List<AppInfoEntity> queryList(Map<String, Object> map) {
        return appInfoDao.queryList(map);
    }

    @Override
    public List<AppInfoEntity> queryListReportFinanceAuditInfo(Map<String, Object> map) {
        return appInfoDao.queryListReportFinanceAuditInfo(map);
    }

    @Override
    public List<AppInfoEntity> queryListJtwf(Map<String, Object> map) {
        return appInfoDao.queryListJtwf(map);
    }

    @Override
    public List<AppInfoEntity> queryListCheck(Map<String, Object> map) {
        return appInfoDao.queryListCheck(map);
    }

    @Override
    public int queryCheckTotal() {
        return appInfoDao.queryCheckTotal();
    }

    @Override
    public int queryCheckTotal(Map<String, Object> map) {
        return appInfoDao.queryCheckTotal(map);
    }

    @Override
    public List<AppFinanceAuditExcelBean> listReportFinanceAuditExcel(Map<String, Object> map) {
        return appInfoDao.listReportFinanceAuditExcel(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return appInfoDao.queryTotal(map);
    }

    @Override
    public int queryTotalReportFinanceAuditInfoNum(Map<String, Object> map) {
        return appInfoDao.queryTotalReportFinanceAuditInfoNum(map);
    }

    @Override
    public void save(AppInfoEntity appInfo) {
        appInfoDao.save(appInfo);
    }

    @Override
    public void update(AppInfoEntity appInfo) {
        appInfoDao.update(appInfo);
        //添加用户消息
        MessageEntity messageEntity = new MessageEntity();
        String applyUserId = appInfo.getUserid();
        messageEntity.setUsername(applyUserId);
        messageEntity.setTitle("审核信息结果");
        String result = "处理中";
        result = AppInfoController.stateTransition(appInfo.getState());
        String remark = "";
        if (StringUtils.isNotBlank(appInfo.getRemark())) {
            remark = appInfo.getRemark();
        }
        String msg = "您的审核信息结果为：" + result;
        if (StringUtils.isNotBlank(appInfo.getRemark())) {
            msg += "\n";
            msg += "审核描述：" + remark;
        }
        messageEntity.setMessage(msg);
        messageEntity.setCreatedate(new Date());
        messageEntity.setType("1");
        messageEntity.setReadstate(0);
        messageDao.save(messageEntity);
        // 发送个推消息
        pushService.pushTransmissionMessageToSingle(messageEntity.getUsername(),messageEntity.getType(), String.valueOf(messageEntity.getId()));
    }

    @Override
    public void delete(String id) {
        appInfoDao.delete(id);
    }

    @Override
    public void deleteBatch(String[] ids) {
        appInfoDao.deleteBatch(ids);
    }

    private AppInfoEntity backAppInfo(AppInfoEntity appInfo) {
        // 设置类型
        appInfo.setIllegalPlateType(appInfo.getVehicleType());
        //替换图片地址协议中出现的两次http://
        if (StringUtils.isNotBlank(appInfo.getIllegalPhoto1())) {
            do {
                appInfo.setIllegalPhoto1(appInfo.getIllegalPhoto1().replace("http://http://", "http://"));
            } while (appInfo.getIllegalPhoto1().contains("http://http://"));
        }
        if (StringUtils.isNotBlank(appInfo.getIllegalPhoto2())) {
            do {
                appInfo.setIllegalPhoto2(appInfo.getIllegalPhoto2().replace("http://http://", "http://"));
            } while (appInfo.getIllegalPhoto2().contains("http://http://"));
        }
        if (StringUtils.isNotBlank(appInfo.getIllegalPhoto3())) {
            do {
                appInfo.setIllegalPhoto3(appInfo.getIllegalPhoto3().replace("http://http://", "http://"));
            } while (appInfo.getIllegalPhoto3().contains("http://http://"));
        }
        // 首次打开详情
        if (StringUtils.isBlank(appInfo.getIllegalPlateType())) {
            return appInfo;
        }
        if (appInfo.getIllegalPlateNumber() != null && !"".equals(appInfo.getIllegalPlateNumber())) {
            // 号牌种类(车辆类型)
            String hpzl = "";
            hpzl = appInfo.getVehicleType();
            List<Vehicle> vehicleInfos = dataInfoQueryService.getVehicleInfoByParams(hpzl, appInfo.getIllegalPlateNumber(), null);
            if (vehicleInfos == null || vehicleInfos.size() == 0) {
                appInfo.setIllegalPlateType("");
                return appInfo;
            }
            for (Vehicle vehicle1 : vehicleInfos) {
                appInfo.setIllegalPlateNumber(vehicle1.getHPHM());//号牌号码
                appInfo.setVehicleBrand(vehicle1.getCLPP1());//车辆品牌
                appInfo.setVehicleType(vehicle1.getHPZL());//车辆类型
                appInfo.setIllegalPlateType(vehicle1.getHPZL());//车辆类型
                appInfo.setVehicleColor(vehicle1.getCSYS());//车辆颜色
                // 显示中文颜色
                String color = appInfo.getVehicleColor();
                if (StringUtils.isNotBlank(color)) {
                    appInfo.setVehicleColor(color
                            .replace("A", "白")
                            .replace("B", "灰")
                            .replace("C", "黄")
                            .replace("D", "粉")
                            .replace("E", "红")
                            .replace("F", "紫")
                            .replace("G", "绿")
                            .replace("H", "蓝")
                            .replace("I", "棕")
                            .replace("J", "黑"));
                }
                appInfo.setVehicleModel(vehicle1.getCLXH());//车辆型号
                appInfo.setIllegalOwnerName(vehicle1.getSYR());//车主姓名
            }
        }
        return appInfo;
    }

}
